<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>代码分析测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        textarea {
            width: 100%;
            height: 300px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-family: monospace;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f9f9f9;
            white-space: pre-wrap;
        }
        .sample-code {
            margin: 10px 0;
            padding: 10px;
            background-color: #f8f8f8;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>代码分析测试</h1>
        
        <h3>示例代码：</h3>
        <div class="sample-code" onclick="useExample(1)">
            <pre>public class UserService {
    private UserRepository userRepository;
    
    public User findUser(String username) {
        return userRepository.findByUsername(username);
    }
    
    public void saveUser(User user) {
        userRepository.save(user);
    }
}</pre>
        </div>
        
        <div class="sample-code" onclick="useExample(2)">
            <pre>public class OrderProcessor {
    public void processOrder(Order order) {
        double total = calculateTotal(order);
        order.setTotal(total);
        saveOrder(order);
        sendNotification(order);
    }
    
    private double calculateTotal(Order order) {
        return order.getItems().stream()
                .mapToDouble(item -> item.getPrice() * item.getQuantity())
                .sum();
    }
    
    private void saveOrder(Order order) {
        // 保存订单
    }
    
    private void sendNotification(Order order) {
        // 发送通知
    }
}</pre>
        </div>

        <h3>输入要分析的代码：</h3>
        <textarea id="code" placeholder="在这里输入或粘贴要分析的代码..."></textarea>
        
        <button onclick="analyzeCode()">分析代码</button>
        
        <h3>分析结果：</h3>
        <div id="result">分析结果将显示在这里...</div>
    </div>

    <script>
        function analyzeCode() {
            const code = document.getElementById('code').value;
            if (!code.trim()) {
                alert('请输入要分析的代码！');
                return;
            }

            fetch('/test/analyze', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ code: code })
            })
            .then(response => response.text())
            .then(result => {
                document.getElementById('result').textContent = result;
            })
            .catch(error => {
                document.getElementById('result').textContent = '分析出错：' + error.message;
            });
        }

        function useExample(number) {
            const codeElement = document.getElementById('code');
            if (number === 1) {
                codeElement.value = document.querySelectorAll('.sample-code pre')[0].textContent;
            } else if (number === 2) {
                codeElement.value = document.querySelectorAll('.sample-code pre')[1].textContent;
            }
        }
    </script>
</body>
</html> 